package org.zhiyootech.base.sdkimpl;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.zhiyootech.sdk.wxpay.IWXPayDomain;
import org.zhiyootech.sdk.wxpay.WXPayConfig;
import org.zhiyootech.sdk.wxpay.WXPayConstants;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;

@Slf4j
@Singleton
public class ZhiYooWxPay extends WXPayConfig {

    @ConfigProperty(name = "app.id")
    String appId;
    @ConfigProperty(name = "app.mch-id")
    String appMchId;
    @ConfigProperty(name = "app.key")
    String appKey;
    @ConfigProperty(name = "app.cert-path")
    String appCertPath;
    // 数字证书
    private byte[] certData;

    @PostConstruct
    public void init() {
        try {
            certData = Files.readAllBytes(Paths.get(new URI("appCertPath")));
        } catch (Exception e) {
            log.error("加载数字证书失败", e);
        }
    }

    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return appMchId;
    }

    @Override
    public String getKey() {
        return appKey;
    }

    @Override
    public InputStream getCertStream() {
        return new ByteArrayInputStream(certData);
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
    }
}
